//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Mobile.Updater;
using System.Diagnostics;
using System.Xml;
using System.Globalization;
using System.IO;

namespace Microsoft.ApplicationBlocks.Mobile.Updater.ActivationProcessors
{
    /// <summary>
    /// Installs a CAB in the device.
    /// If config data is provided, installs the file indicated in "source".
    /// Otherwise looks for .CAB files in the entire manifest 
    /// and installs these.
    /// </summary>
    public class CABUpdateProcessor : IActivationProcessor
    {
        private List<string> filesToInstall = new List<string>();

        #region IActivationProcessor Members

        public void Init(Xsd.ActivationProcessorProviderData data, UpdaterTask task)
        {
            if (data.AnyAttr == null)
            {
                foreach (FileManifest file in task.Manifest.Files)
                {
                    filesToInstall.Add(Path.Combine(task.DownloadFilesBase, file.Source));
                }
            }
            else
            {
                // Validate the source parameter
                foreach (XmlAttribute attr in data.AnyAttr)
                {
                    if (String.Compare(attr.Name, "source", false, CultureInfo.InvariantCulture) == 0)
                    {
                        filesToInstall.Add(attr.Value);
                    }
                }
            }

            if (filesToInstall.Count == 0)
            {
                throw new ArgumentException("Source argument expected");
            }
        }

        public void PrepareExecution()
        {
            // XXX Shouldn't the updater at this point close the application if running?
            //RunningApplicationsManager.Instance.SendCloseMessage(task.Manifest.Application.ApplicationId);
        }

        public void Execute()
        {
            // TODO: Use sub-launching, as the CAB installer might finish before the Updater gets notification
            foreach (string fileToInstall in filesToInstall)
            {
                Process s = Process.Start(fileToInstall, "");
                s.WaitForExit();
            }
        }

        public void OnError()
        {
            // TODO: implement rollback
        }

        #endregion
    }
}
